home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Libraries / CW GUSI 1.6.4 / doc / pod / GUSI_Common.pod < prev    next >
Text File  |  1995-04-20  |  16KB  |  594 lines

  1. =head1 Overview
  2.  
  3. This section discusses the routines common to all, or almost all
  4. communication domains. These routines return C<-1> if an error occurred,
  5. and set the variable C<errno> to an error code. On success, the routines return C<0>
  6. or some positive value.
  7.  
  8. Here's a list of all error codes and their typical explanations. The most important
  9. of them are repeated for the individual calls.
  10.  
  11. =over 4
  12.  
  13. =item C<EACCES>
  14.  
  15. Permission denied: An attempt was made to access a file in a way forbidden by its
  16. file access permissions, e.g., to C<open()> a locked file for writing.
  17.  
  18. =item C<EADDRINUSE>
  19.  
  20. Address already in use: C<bind()> was called with an address already in use 
  21. by another socket.
  22.  
  23. =item C<EADDRNOTAVAIL>
  24.  
  25. Can't assign requested address: C<bind()> was called with an address which this 
  26. socket can't assume, e.g., a TCP/IP address whose C<in_addr> specifies a different 
  27. host.
  28.  
  29. =item C<EAFNOSUPPORT>
  30.  
  31. Address family not supported: You haven't linked with this socket family or have
  32. specified a nonexisting family, e.g., AF_CHAOS.
  33.  
  34. =item C<EALREADY>
  35.  
  36. Operation already in progress, e.g., C<connect()> was called twice in a row for a 
  37. nonblocking socket.
  38.  
  39. =item C<EBADF>
  40.  
  41. Bad file descriptor: The file descriptor you specified is not open.
  42.  
  43. =item C<EBUSY>
  44.  
  45. Request for a system resource already in incompatible use, e.g., attempt to delete
  46. an open file.
  47.  
  48. =item C<ECONNREFUSED>
  49.  
  50. Connection refused, e.g. you specified an unused port for a C<connect()>
  51.  
  52. =item C<EEXIST>
  53.  
  54. File exists, and you tried to open it with C<O_EXCL>.
  55.  
  56. =item C<EHOSTDOWN>
  57.  
  58. Remote host is down.
  59.  
  60. =item C<EHOSTUNREACH>
  61.  
  62. No route to host.
  63.  
  64. =item C<EINPROGRESS>
  65.  
  66. Operation now in progress. This is *not* an error, but returned from nonblocking 
  67. operations, e.g., nonblocking C<connect()>.
  68.  
  69. =item C<EINTR>
  70.  
  71. Interrupted system call: The user pressed Command-. or C<alarm()> timed out.
  72.  
  73. =item C<EINVAL>
  74.  
  75. Invalid argument or various other error conditions.
  76.  
  77. =item C<EIO>
  78.  
  79. Input/output error.
  80.  
  81. =item C<EISCONN>
  82.  
  83. Socket is already connected.
  84.  
  85. =item C<EISDIR>
  86.  
  87. Is a directory, e.g. you tried to C<open()> a directory.
  88.  
  89. =item C<EMFILE>
  90.  
  91. Too many open files.
  92.  
  93. =item C<EMSGSIZE>
  94.  
  95. Message too long, e.g. for an UDP C<send()>.
  96.  
  97. =item C<ENAMETOOLONG>
  98.  
  99. File name too long.
  100.  
  101. =item C<ENETDOWN>
  102.  
  103. Network is down, e.g., Appletalk is turned off in the chooser.
  104.  
  105. =item C<ENFILE>
  106.  
  107. Too many open files in system.
  108.  
  109. =item C<ENOBUFS>
  110.  
  111. No buffer space available.
  112.  
  113. =item C<ENOENT>
  114.  
  115. No such file or directory.
  116.  
  117. =item C<ENOEXEC>
  118.  
  119. Severe error with the PowerPC standard library.
  120.  
  121. =item C<ENOMEM>
  122.  
  123. Cannot allocate memory.
  124.  
  125. =item C<ENOSPC>
  126.  
  127. No space left on device.
  128.  
  129. =item C<ENOTCONN>
  130.  
  131. Socket is not connected, e.g., neither C<connect()> nor C<accept()> has been called 
  132. successfully for it.
  133.  
  134. =item C<ENOTDIR>
  135.  
  136. Not a directory.
  137.  
  138. =item C<ENOTEMPTY>
  139.  
  140. Directory not empty, e.g., attempt to delete nonempty directory.
  141.  
  142. =item C<ENXIO>
  143.  
  144. Device not configured, e.g., MacTCP control panel misconfigured.
  145.  
  146. =item C<EOPNOTSUPP>
  147.  
  148. Operation not supported on socket, e.g., C<sendto()> on a stream socket.
  149.  
  150. =item C<EPFNOSUPPORT>
  151.  
  152. Protocol family not supported, i.e., attempted use of ADSP on a machine that has 
  153. AppleTalk but not ADSP.
  154.  
  155. =item C<EPROTONOSUPPORT>
  156.  
  157. Protocol not supported, e.g., you called C<getprotobyname()> with neither "tcp" nor
  158. "udp" specified.
  159.  
  160. =item C<ERANGE>
  161.  
  162. Result too large, e.g., C<getcwd()> called with insufficient buffer.
  163.  
  164. =item C<EROFS>
  165.  
  166. Read-only file system.
  167.  
  168. =item C<ESHUTDOWN>
  169.  
  170. Can't send after socket shutdown.
  171.  
  172. =item C<ESOCKTNOSUPPORT>
  173.  
  174. Socket type not supported, e.g., datagram PPC toolbox sockets.
  175.  
  176. =item C<ESPIPE>
  177.  
  178. Illegal seek, e.g., C<lseek()> called for a TCP socket.
  179.  
  180. =item C<EWOULDBLOCK>
  181.  
  182. Nonblocking operation would block.
  183.  
  184. =item C<EXDEV>
  185.  
  186. Cross-device link, e.g. C<FSpSmartMove()> attempted to move file to a different 
  187. volume.
  188.  
  189. =back
  190.  
  191. =head2 Creating and destroying sockets
  192.  
  193. A socket is created with C<socket()> and destroyed
  194. with C<close()>.
  195.  
  196. C<int socket(int af, int type, int protocol)> creates an endpoint for communication
  197. and returns a descriptor. C<af> specifies the communication domain to be used. Valid
  198. values are:
  199.  
  200. =over 4
  201.  
  202. =item C<AF_UNIX>
  203.  
  204. Communication internal to a single Mac.
  205.  
  206. =item C<AF_INET>
  207.  
  208. TCP/IP, using C<MacTCP>.
  209.  
  210. =item C<AF_APPLETALK>
  211.  
  212. Appletalk, using ADSP.
  213.  
  214. =item C<AF_PPC>
  215.  
  216. The Program-to-Program Communication Toolbox.
  217.  
  218. =back
  219.  
  220. C<type> specifies the semantics of the communication. The following two types are
  221. available:
  222.  
  223. =over 4
  224.  
  225. =item C<SOCK_STREAM>
  226.  
  227. A two way, reliable, connection based byte stream.
  228.  
  229. =item C<SOCK_DGRAM>
  230.  
  231. Connectionless, unreliable messages of a fixed maximum length.
  232.  
  233. =back
  234.  
  235. C<protocol> would be used to specify an alternate protocol to be used with a socket.
  236. In C<GUSI>, however, this parameter is always ignored.
  237.  
  238. Error codes:
  239.  
  240. =over 4
  241.  
  242. =item C<EINVAL>
  243.  
  244. The C<af> you specified doesn't exist.
  245.  
  246. =item C<EMFILE>
  247.  
  248. The descriptor table is full.
  249.  
  250. =back
  251.  
  252. C<void close(int fd)> removes the access path associated with the descriptor, and
  253. closes the file or socket if the last access path referring to it was removed.
  254.  
  255. =head2 Prompting the user for an address
  256.  
  257. To give the user the opportunity of entering
  258. an address for a socket to be bound or connected to, the C<choose()> routine was
  259. introduced in C<GUSI>. This routine has no counterpart in UNIX implementations.
  260.  
  261. C<int choose(int dom, int type, char * prompt, void * constraint, int flags,
  262. void * name, int * nlen)> puts up a modal dialog prompting the user to
  263. choose an address. C<dom> specifies the communication domain, like in C<socket>.
  264. C<type> may be used by future communication domains to further differentiate
  265. within a domain, but is ignored by current domains. C<prompt> is a message that will
  266. appear in the dialog. C<constraint> may be used to restrict the types of acceptable
  267. addresses (For more information, consult the section of the communication domain).
  268. The following two C<flags> are defined for most socket types:
  269.  
  270. =over 4
  271.  
  272. =item C<CHOOSE_DEFAULT>
  273.  
  274. Offer the contents passed in C<name> as the default choice.
  275.  
  276. =item C<CHOOSE_NEW>
  277.  
  278. Prompt for a new address, suitable for passing to C<bind()>. Default
  279. is prompting for an existing address, to be used by C<connect()>.
  280.  
  281. =back
  282.  
  283. C<name> on input contains a default address if C<CHOOSE_DEFAULT> is set. On output, it
  284. is set to the address chosen.
  285.  
  286. Error codes:
  287.  
  288. =over 4
  289.  
  290. =item C<EINVAL>
  291.  
  292. One of the C<flags> is not (yet) supported by this communications
  293. domain. This error is never reported for C<CHOOSE_DEFAULT>, which might get silently
  294. ignored.
  295.  
  296. =item C<EINTR>
  297.  
  298. The user chose "Cancel" in the dialog.
  299.  
  300. =back
  301.  
  302. =head2 Establishing connections between sockets
  303.  
  304. Before you can transmit data on a
  305. stream socket, it must be connected to a peer socket. Connection establishment is
  306. asymmetrical: The server socket registers its address with C<bind()>, calls C<listen()>
  307. to indicate its willingness to accept connections and accepts them by calling
  308. C<accept()>. The client socket, after possibly having registered its address with
  309. C<bind()> (This is not necessary for all socket families as some will automatically
  310. assign an address) calls C<connect()> to establish a connection with a server.
  311.  
  312. It is possible, but not required, to call C<connect()> for datagram sockets.
  313.  
  314. C<int bind(int s, const struct sockaddr *name, int namelen)> binds a socket to its address. The
  315. format of the address is different for every socket family. For some families, you
  316. may ask the user for an address by calling C<choose()>.
  317.  
  318. Error codes:
  319.  
  320. =over 4
  321.  
  322. =item C<EAFNOSUPPORT>
  323.  
  324. C<name> specifies an illegal address family for this socket.
  325.  
  326. =item C<EADDRINUSE>
  327.  
  328. There is already another socket with this address.
  329.  
  330. =back
  331.  
  332. C<int listen(int s, int qlen)> turns a socket into a listener. C<qlen> determines
  333. how many sockets can concurrently wait for a connection, but is ignored for almost
  334. all socket families.
  335.  
  336. C<int accept(int s, struct sockaddr *addr, int *addrlen)> accepts a connection for a socket
  337. I<on a new socket> and returns the descriptor of the new socket. If C<addr> is not
  338. C<NULL>, the address of the connecting socket will be assigned to it.
  339.  
  340. You can find out if a connection is pending by calling C<select()> to find out if
  341. the socket is ready for I<reading>.
  342.  
  343. Error codes:
  344.  
  345. =over 4
  346.  
  347. =item C<ENOTCONN>
  348.  
  349. You did not call C<listen()> for this socket.
  350.  
  351. =item C<EWOULDBLOCK>
  352.  
  353. The socket is nonblocking and no socket is trying to connect.
  354.  
  355. =back
  356.  
  357. C<int connect(int s, const struct sockaddr *addr, int addrlen)> tries to connect to the socket whose
  358. address is in C<addr>. If the socket is nonblocking and the connection cannot be
  359. made immediately, C<connect()> returns C<EINPROGRESS>. You can find out if the
  360. connection has been established by calling C<select()> to find out if
  361. the socket is ready for I<writing>.
  362.  
  363. Error codes:
  364.  
  365. =over 4
  366.  
  367. =item C<EAFNOSUPPORT>
  368.  
  369. C<name> specifies an illegal address family for this socket.
  370.  
  371. =item C<EISCONN>
  372.  
  373. The socket is already connected.
  374.  
  375. =item C<EADDRNOAVAIL>
  376.  
  377. There is no socket with the given address.
  378.  
  379. =item C<ECONNREFUSED>
  380.  
  381. The socket refused the connection.
  382.  
  383. =item C<EINPROGRESS>
  384.  
  385. The socket is nonblocking and the connection is being established.
  386.  
  387. =back
  388.  
  389. =head2 Transmitting data between sockets
  390.  
  391. You can write data to a socket using C<write()>,
  392. C<writev()>, C<send()>, C<sendto()>, or C<sendmsg()>. You can read data from a socket
  393. using C<read()>, C<readv()>, C<recv()>, C<recvfrom()>, or C<recvmsg()>.
  394.  
  395. C<int read(int s, char *buffer, unsigned buflen)> reads up to C<buflen> bytes from
  396. the socket. C<read()> for sockets differs from C<read()> for files mainly in that it
  397. may read fewer than the requested number of bytes without waiting for the rest to
  398. arrive.
  399.  
  400. Error codes:
  401.  
  402. =over 4
  403.  
  404. =item C<EWOULDBLOCK>
  405.  
  406. The socket is nonblocking and there is no data immediately available.
  407.  
  408. =back
  409.  
  410. C<int readv(int s, const struct iovec *iov, int count)> performs the same
  411. action, but scatters the input data into the C<count> buffers of the C<iov> array, always
  412. filling one buffer completely before proceeding to the next. C<iovec> is defined as
  413. follows:
  414.  
  415.     struct iovec {
  416.       caddr_t  iov_base;   /* Address of this buffer */
  417.       int   iov_len;       /* Length of the buffer */
  418.     };
  419.  
  420. C<int recv(int s, void *buffer, int buflen, int flags)> is identical to C<read()>,
  421. except for the C<flags> parameter. If the C<MSG_OOB> flag is set for a stream socket
  422. that supports out-of-band data, C<recv()> reads out-of-band data.
  423.  
  424. C<int recvfrom(int s, void *buffer, int buflen, int flags, void *from, int *fromlen)>
  425. is the equivalent of C<recv()> for unconnected datagram sockets. If C<from> is not
  426. C<NULL>, it will be set to the address of the sender of the message.
  427.  
  428. C<int recvmsg(int s, struct msghdr *msg, int flags)> is the most general routine,
  429. combining the possibilities of C<readv()> and C<recvfrom()>. C<msghdr> is defined as
  430. follows:
  431.  
  432.     struct msghdr {
  433.       caddr_t  msg_name;            /* Like from in recvfrom() */
  434.       int      msg_namelen;         /* Like fromlen in recvfrom() */
  435.       struct   iovec *msg_iov;      /* Scatter/gather array */
  436.       int      msg_iovlen;          /* Number of elements in msg_iov */
  437.       caddr_t  msg_accrights;       /* Access rights sent/received. Not used in GUSI */
  438.       int      msg_accrightslen;
  439.     };
  440.  
  441. C<int write(int s, char *buffer, unsigned buflen)> writes up to C<buflen> bytes to
  442. the socket. As opposed to C<read()>, C<write()> for nonblocking sockets always blocks
  443. until all bytes are written or an error occurs.
  444.  
  445.  
  446. Error codes:
  447.  
  448. =over 4
  449.  
  450. =item C<EWOULDBLOCK>
  451.  
  452. The socket is nonblocking and data can't be immediately written.
  453.  
  454. =back
  455.  
  456. C<int writev(int s, const struct iovec *iov, int count)> performs the same
  457. action, but gathers the output data from the C<count> buffers of the C<iov> array, always
  458. sending one buffer completely before proceeding to the next.
  459.  
  460. C<int send(int s, void *buffer, int buflen, int flags)> is identical to C<write()>,
  461. except for the C<flags> parameter. If the C<MSG_OOB> flag is set for a stream socket
  462. that supports out-of-band data, C<send()> sends an out-of-band message.
  463.  
  464. C<int sendto(int s, void *buffer, int buflen, int flags, void *to, int *tolen)>
  465. is the equivalent of C<send()> for unconnected datagram sockets. The message will
  466. be sent to the socket whose address is given in C<to>.
  467.  
  468. C<int sendmsg(int s, const struct msghdr *msg, int flags)> combines the possibilities
  469. of C<writev()> and C<sendto()>.
  470.  
  471. =head2 I/O multiplexing
  472.  
  473.  
  474. int select(int width, fd_set *readfds, fd_set *writefds,
  475. fd_set *exceptfds, struct timeval *timeout) examines the I/O descriptors specified
  476. by the bit masks C<readfs>, C<writefs>, and C<exceptfs> to see if they are ready for
  477. reading, writing, or have an exception pending. C<width> is the number of significant
  478. bits in the bit mask. C<select()> replaces the bit masks with masks of those descriptors
  479. which are ready and returns the total number of ready descriptors. C<timeout>, if
  480. not C<NULL>, specifies the maximum time to wait for a descriptor to become ready. If
  481. C<timeout> is C<NULL>, C<select()> waits indefinitely. To do a poll, pass a pointer to
  482. a zero C<timeval> value in C<timeout>. Any of C<readfds>, C<writefds>, or C<exceptfds>
  483. may be given as C<NULL> if no descriptors are of interest.
  484.  
  485. Error codes:
  486.  
  487. =over 4
  488.  
  489. =item C<EBADF>
  490.  
  491. One of the bit masks specified an invalid descriptor.
  492.  
  493. =back
  494.  
  495. The descriptor bit masks can be manipulated with the following macros:
  496.  
  497.  
  498.     FD_ZERO(fds);    /* Clear all bits in *fds */
  499.     FD_SET(n, fds);  /* Set bit n in *fds */
  500.     FD_CLR(n, fds);  /* Clear bit n in *fds */
  501.     FD_ISSET(n, fds);   /* Return 1 if bit n in *fds is set, else 0 */
  502.  
  503. =head2 Getting and changing properties of sockets
  504.  
  505. You can obtain the address of a
  506. socket and the socket it is connected to by calling C<getsockname()> and
  507. C<getpeername()> respectively. You can query and manipulate other properties of a
  508. socket by calling C<ioctl()>, C<fcntl()>, C<getsockopt()>, and C<setsockopt()>. You
  509. can create additional descriptors for a socket by calling C<dup()> or C<dup2()>.
  510.  
  511. C<int getsockname(int s, struct sockaddr *name, int *namelen)> returns in C<*name> the address
  512. the socket is bound to. C<*namelen> should be set to the maximum length of C<name> and
  513. will be set by C<getsockname()> to the actual length of the name.
  514.  
  515. C<int getpeername(int s, struct sockaddr *name, int *namelen)> returns in C<*name> the address
  516. of the socket that this socket is connected to. C<*namelen> should be set to the
  517. maximum length of C<name> and will be set by C<getpeername()> to the actual length
  518. of the name.
  519.  
  520. C<int ioctl(int d, unsigned int request, long *argp)> performs various operations
  521. on the socket, depending on the C<request>. The following codes are valid for all
  522. socket families:
  523.  
  524. =over 4
  525.  
  526. =item C<FIONBIO>
  527.  
  528. Make the socket blocking if the C<long> pointed to by argp is C<0>, else
  529. make it nonblocking.
  530.  
  531. =item C<FIONREAD>
  532.  
  533. Set C<*argp> to the number of bytes waiting to be read.
  534.  
  535. =back
  536.  
  537. Error codes:
  538.  
  539. =over 4
  540.  
  541. =item C<EOPNOTSUPP>
  542.  
  543. The operation you requested with C<request> is not supported by this
  544. socket family.
  545.  
  546. =back
  547.  
  548. C<int fcntl(int s, unsigned int cmd, int arg)> provides additional control over a
  549. socket. The following values of C<cmd> are defined for all socket families:
  550.  
  551. =over 4
  552.  
  553. =item C<F_DUPFD>
  554.  
  555. Return a new descriptor greater than or equal to C<arg> which refers to the same socket.
  556.  
  557. =item C<F_GETFL>
  558.  
  559. Return descriptor status flags.
  560.  
  561. =item C<F_SETFL>
  562.  
  563. Set descriptor status flags to C<arg>.
  564.  
  565. =back
  566.  
  567. The only status flag implemented is C<FNDELAY> which is true if the socket is
  568. nonblocking.
  569.  
  570. Error codes:
  571.  
  572. =over 4
  573.  
  574. =item C<EOPNOTSUPP>
  575.  
  576. The operation you requested with C<cmd> is not supported by this
  577. socket family.
  578.  
  579. =back
  580.  
  581. C<int getsockopt(int s, int level, int optname, void *optval, int * optlen)> is
  582. used to request information about sockets. It is not implemented in C<GUSI>.
  583.  
  584. C<int setsockopt(int s, int level, int optname, void *optval, int optlen)> is used
  585. to set options associated with a socket. It is not implemented in C<GUSI>.
  586.  
  587. C<int dup(int fd)> returns a new descriptor referring to the same socket as C<fd>.
  588. The old and new descriptors are indistinguishible. The new descriptor will always
  589. be the smallest free descriptor.
  590.  
  591. C<int dup2(int oldfd, int newfd)> closes C<newfd> if it was open and makes it
  592. a duplicate of C<oldfd>. The old and new descriptors are indistinguishible.
  593.  
  594.